import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
dayjs.extend(utc)
export function formatUTC(utcString: string) {
  const resultTime = dayjs.utc(utcString).format('YYYY/MM/DD HH:mm:ss')
  return resultTime
}

//utcString: 2023-03-02T04:12:58.000Z
//   xxx   : yyyyMMddhhmmss  / yyyy-MM-dd hh:mm:ss
//  PUNC   : '-' || '/' ||' '
export function formatTime(utcString: string, xxx: string = 'yyyy-MM-dd hh:mm:ss') {
  function fn(val: any) {
    return val < 10 ? '0' + val : val
  }
  function sp(val: any) {
    return val?.split('')
  }
  let arr = ['getFullYear', 'getMonth', 'getDate', 'getHours', 'getMinutes', 'getSeconds']
  let str = ''
  const key: any = new Date(utcString)
  const string = sp(xxx)
  const reg: any = /^[a-zA-Z]+$/ // /[a-zA-Z]+/  // /^[a-zA-Z]+$/
  const PUNC: any = []
  if (xxx) {
    arr = []
    new Set(string).forEach((item, index) => {
      if (item === 'y') {
        arr.push('getFullYear')
      } else if (item === 'M') {
        arr.push('getMonth')
      } else if (item === 'd') {
        arr.push('getDate')
      } else if (item === 'h') {
        arr.push('getHours')
      } else if (item === 'm') {
        arr.push('getMinutes')
      } else if (item === 's') {
        arr.push('getSeconds')
      }
    })
    let i = 0
    string.map((item: string, index: any) => {
      if (index > 0 && item !== string[index - 1] && reg.test(item) && reg.test(string[index - 1])) {
        PUNC[i] = ''
        i++
      } else if (!reg.test(item) && reg.test(string[index - 1])) {
        PUNC[i] = item
        i++
      }
    })
    // console.log(PUNC)
  }
  arr.forEach((item, index) => {
    const time = key[item]()
    if (index < arr.length - 1) {
      str += (item !== 'getMonth' ? fn(time) : fn(time + 1)) + (PUNC[index] ?? '')
    } else {
      str += item !== 'getMonth' ? fn(time) : fn(time + 1)
    }
    // console.log(fn(time), str)
  })
  return str
}

export function time() {
  const obj = 'yyyy-MM-dd hh:mm:ss'
  const time = obj.split('')
  const reg = /[a-zA-Z]+/
  let i = 0
  let j = 0
  time.forEach((item) => {
    if (!reg.test(item)) {
      i++
      console.log(i)
    } else {
      j++
      console.log(j)
    }
    // console.log(reg.test(item))
  })
}
